<?php

namespace app\admin\model\dividend;


use think\Model;
use think\Db;
use think\Session;
/**
 * 公司模型
 */
class Dividend extends Model{
    protected $name = 'yonghui_company';
    /**
     * 获取公司列表
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($serach,$type){
        //查找当前登录账号所属的自己公司
        $session=Session::get('yoshop_adminuser');
        if($session['user']['type']==1){
        }else{
            $where['upId']=['=',$session['user']['company_id']];
        }
        if(!empty($serach['search'])){
            $where['name|legalName']=['like','%'.$serach['search'].'%'];
        }
        $where['type']=['=',$type];
        return $this->with(['image'])
            ->where($where)
            ->order(['createTime' => 'desc'])
            ->paginate(10, false, [
                'query' => request()->request()
            ]);

    }
    public static function detail($id){
        return self::get($id, ['image']);
    }
    public function image(){
        return $this->hasOne('uploadFile', 'file_id', 'licenseId');
    }
    /**
     * 新增记录
     */
    public function add($data,$admin){
        $session=Session::get('yoshop_adminuser');
        if (empty($admin['name'])) {
            $this->error = '登录名称不能为空';
            return false;
        }
        if($session['user']['type']!=1){
            if (empty($data['userId'])) {
                $this->error = '请选择渠道经理';
                return false;
            }
        }
        if (empty($admin['password'])) {
            $this->error = '登录密码不能为空';
            return false;
        }
        if (empty($admin['repassword'])) {
            $this->error = '确认密码不能为空';
            return false;
        }
        if ($admin['repassword']!==$admin['password']) {
            $this->error = '两次输入密码不一致';
            return false;
        }
        if (empty($data['name'])) {
            $this->error = '公司名称不能为空';
            return false;
        }
        if (empty($data['legalName'])) {
            $this->error = '法人姓名不能为空';
            return false;
        }
        if (empty($data['phone'])) {
            $this->error = '联系电话不能为空';
            return false;
        }
        if (empty($data['licenseId'])) {
            $this->error = '请上传营业执照图片';
            return false;
        }

        if($session['user']['type']==2){
            //登录账号为自营公司
            //$companyInfos=Db::name('yonghui_company')->where(['name'=>$data['name'],'upId'=>$session['user']['company_id']])->whereOr(['shuihao'=>$data['shuihao'],'upId'=>$session['user']['company_id']])->find();
            $companyInfos=Db::name('yonghui_company')->where(['name'=>$data['name'],'upId'=>$session['user']['company_id']])->field('id')->find();
            if($companyInfos){
                $this->error = '该渠道公司名称已被添加过';
                return false;
            }
            $companyInfos1=Db::name('yonghui_company')->where(['shuihao'=>$data['shuihao'],'upId'=>$session['user']['company_id']])->field('id')->find();
            if($companyInfos1){
                $this->error = '该渠道公司税号已被添加过';
                return false;
            }
            $companyInfos2=Db::name('yonghui_company')->where(['phone'=>$data['phone'],'upId'=>$session['user']['company_id']])->field('id')->find();
            if($companyInfos2){
                $this->error = '该渠道公司手机号已被添加过';
                return false;
            }
        }else{
            //登录账号为平台   确认该公司和该账户是否已经添加过
            $companyInfo=Db::name('yonghui_company')->where(['name'=>$data['name']])->whereOr(['shuihao'=>$data['shuihao']])->find();
            if($companyInfo){
                if($companyInfo['name']==$data['name'] && $companyInfo['shuihao']!=$data['shuihao']){
                    $this->error = '该公司已被添加过';
                    return false;
                }
                if($companyInfo['name']!=$data['name'] && $companyInfo['shuihao']==$data['shuihao']){
                    $this->error = '该税号已被添加过';
                    return false;
                }
                if($companyInfo['name']==$data['name'] && $companyInfo['shuihao']==$data['shuihao']){
                    $this->error = '该公司名称和税号已被添加过';
                    return false;
                }
            }
        }
        $adminUser=Db::name('yonghui_adminuser')->where(['name'=>$admin['name']])->find();
        if($adminUser){
            $this->error = '该登录账号已被添加过,请重新输入';
            return false;
        }
        //根据上级账号类型确认 公司性质。
        if($session['user']['type']==1){
            $data['upId']=0;
            $data['userId']=0;
            $data['type']=1;
            $type=2;
        }else{
            $data['upId']=$session['user']['company_id'];
            $data['type']=2;
            $type=3;
        }
        $data['createTime']=time();
        $data['updateTime']=0;
        $data['status']=1;
        $ids=Db('yonghui_company')->insertGetId($data);
        if($ids){
            $admins['companyId']=$ids;
            $admins['name']=$admin['name'];
            $admins['phone']=$data['phone'];
            $admins['password']=md5($admin['password']);
            $admins['type']=$type;
            $admins['createTime']=time();
            $admins['updateTime']=0;
            $admins['status']=1;
            $res=Db('yonghui_adminuser')->insertGetId($admins);
            return $res;
        }else{
            return false;
        }
    }

    /**
     * 更新记录
     */
    public function edit($data,$id){
        $data['updateTime']=time();
        $data['status']=1;
        if (empty($data['name'])) {
            $this->error = '公司名称不能为空';
            return false;
        }

        if(empty($data['legalName'])){
            $this->error = '法人姓名不能为空';
            return false;
        }
        if(empty($data['phone'])){
            $this->error = '联系电话不能为空';
            return false;
        }
        if(empty($data['licenseId'])){
            $this->error = '请上传营业执照图片';
            return false;
        }
        $companyInfo=Db::name('yonghui_company')->where(['name'=>$data['name']])->whereOr(['shuihao'=>$data['shuihao']])->find();
        //var_dump($id);var_dump($companyInfo['id']);
        if($companyInfo && $id!=$companyInfo['id']){
            if($companyInfo['name']==$data['name'] && $companyInfo['shuihao']!=$data['shuihao']){
                $this->error = '该公司已被添加过';
                return false;
            }
            if($companyInfo['name']!=$data['name'] && $companyInfo['shuihao']==$data['shuihao']){
                $this->error = '该税号已被添加过';
                return false;
            }
            if($companyInfo['name']==$data['name'] && $companyInfo['shuihao']==$data['shuihao']){
                $this->error = '该公司名称和税号已被添加过';
                return false;
            }
        }
        return $this->allowField(true)->save($data) !== false;
    }

    /**
     * 软删除
     * @return false|int
     */
    public function setDelete(){
        return $this->allowField(true)->save(['status' => 2]);
    }
    /**
     * 恢复
     * @return false|int
     */
    public function setrecover(){
        return $this->allowField(true)->save(['status' => 1]);
    }

}